package com.puti.doctor.controller;

import com.common.oss.service.OssService;
import com.puti.common.core.web.controller.BaseController;
import com.puti.common.core.web.domain.AjaxResult;
import com.puti.common.core.web.page.TableDataInfo;
import com.puti.doctor.domian.HospitalDoctorInfo;
import com.puti.doctor.domian.PatientInfo;
import com.puti.doctor.service.AccountManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/account")
public class AccountManagementController extends BaseController {

    @Autowired
    private AccountManagementService accountManagementService;

//    @Autowired
    private OssService ossService;

    public AccountManagementController(OssService ossService) {
        this.ossService = ossService;
    }


    //账号管理-医生列表
    @GetMapping("/selectHospitalDoctorInfoList")
    public TableDataInfo selectHospitalDoctorInfoList(HospitalDoctorInfo hospitalDoctorInfo){
        startPage();
        List<HospitalDoctorInfo> hospitalDoctorInfos = accountManagementService.selectHospitalDoctorInfoList(hospitalDoctorInfo);
        return getDataTable(hospitalDoctorInfos);
    }

    //账号管理-医生列表-添加医生
    @PostMapping("/insertHospitalDoctorInfo")
    public Integer insertHospitalDoctorInfo(HospitalDoctorInfo hospitalDoctorInfo,
                                               @RequestParam("file") MultipartFile file,
                                               HttpServletRequest request){
        String ossFilePath = ossService.uploadMultipartFile(file);
//        String path = "D:/img";
//        //上传文件名
//        String filename = file.getOriginalFilename();
//        File file1 = new File(path, filename);
//        if(!file1.getParentFile().exists()){
//            file1.getParentFile().mkdir();
//        }
//        try {
//            file.transferTo(file1);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        hospitalDoctorInfo.setDoctorQualifications(filename);
        return accountManagementService.insertHospitalDoctorInfo(hospitalDoctorInfo);
    }

    //账号管理-医生列表-删除医生
    @DeleteMapping("/deleteHospitalDoctorInfoByDoctorId")
    public AjaxResult deleteHospitalDoctorInfoByDoctorId(Integer doctorId){
        return toAjax(accountManagementService.deleteHospitalDoctorInfoByDoctorId(doctorId));
    }

    @GetMapping("/selectHospitalDoctorInfoByDoctorId")
    //根据doctorId进行数据回显
    public AjaxResult selectHospitalDoctorInfoByDoctorId(Integer doctorId){
        return AjaxResult.success(accountManagementService.selectHospitalDoctorInfoByDoctorId(doctorId));
    }

    //账号管理-医生列表-修改医生
    @PutMapping("/updateHospitalDoctorInfo")
    public AjaxResult updateHospitalDoctorInfo(@RequestBody HospitalDoctorInfo hospitalDoctorInfo) {
        return toAjax(accountManagementService.updateHospitalDoctorInfo(hospitalDoctorInfo));
    }

    //账号管理-患者列表
    @GetMapping("/selectPatientInfoList")
    public TableDataInfo selectPatientInfoList(PatientInfo patientInfo){
        startPage();
        List<PatientInfo> patientInfos = accountManagementService.selectPatientInfoList(patientInfo);
        return getDataTable(patientInfos);
    }

}
